package com.zerui.manager.app.device.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.zerui.cola.dto.Response;
import com.zerui.manager.device.Device;
import com.zerui.manager.device.domainservice.DeviceRepository;
import com.zerui.manager.web.cmd.DeviceUpdateCmd;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @Author wh
 * @create_time 2023-08-25 13:54
 */
@Component
@CatchAndLog
public class DeviceUpdateCmdExe {

    @Resource
    private DeviceRepository deviceRepository;

    public Response execute(DeviceUpdateCmd deviceUpdateCmd) {

        Device device = deviceRepository.getByDeviceId(deviceUpdateCmd.getDeviceId());
        if(ObjectUtils.isNotEmpty(device)){
            device.setAccess(deviceUpdateCmd.getAccess());
            deviceRepository.update(device);
            return Response.buildSuccess();
        }
        deviceRepository.add(Device.create(deviceUpdateCmd.getDeviceId(), 0, deviceUpdateCmd.getAccess()));
        return Response.buildSuccess();
    }
}
